﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NMF.Optimizations
{
    public class DynamicModificationPattern : ModificationPattern, ISourceObserver<IOptimizationModification>
    {
        private List<IOptimizationModification> modifications = new List<IOptimizationModification>();

        public DynamicModificationPattern(IObservableSource<IOptimizationModification> pattern)
        {
            if (pattern == null) throw new ArgumentNullException("pattern");

            foreach (var mod in pattern)
            {
                modifications.Add(mod);
            }

            pattern.Subscribe(this);
        }

        public override IEnumerable<IOptimizationModification> GetCurrentModifications()
        {
            return modifications;
        }

        public void OnNewItem(IOptimizationModification item)
        {
            modifications.Add(item);
        }

        public void OnUpdatedItem(IOptimizationModification item)
        {
        }

        public void OnDeletedItem(IOptimizationModification item)
        {
            modifications.Remove(item);
        }

        public void OnReset()
        {
            modifications.Clear();
        }
    }
}
